/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SystemManager.User.view;
import SystemManager.view.TableModelFactory;
import SystemManager.User.logic.AddUserManage;
import SystemManager.view.SystemManagerFrame;
import data.pojo.User;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author Administrator
 */
public class UserManagePanel extends javax.swing.JPanel {

    /**
     * Creates new form AddUserPanel
     */
    public UserManagePanel(){
        initComponents();
        refreshSearchTable("id","");
        this.setLocation(380, 220);
    }
    public UserManagePanel(SystemManagerFrame parent) {
        SMF=parent;
        initComponents();
       
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        jButton1.setText("添加");

        jButton2.setText("修改职位");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("删除");

        jButton4.setText("提交");

        jTable1.setModel(userTable);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(24, 24, 24)
                .add(jButton1)
                .add(68, 68, 68)
                .add(jButton2)
                .add(92, 92, 92)
                .add(jButton3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 97, Short.MAX_VALUE)
                .add(jButton4)
                .add(41, 41, 41))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton1)
                    .add(jButton2)
                    .add(jButton3)
                    .add(jButton4))
                .addContainerGap())
        );

        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton4MouseClicked(evt);
            }
        });
    }// </editor-fold>//GEN-END:initComponents
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
        User nbook,temp;
        nbook= new AddUserDialog(null, true).showDialog();
        temp=nbook;
          // System.out.println("输入的 id"+temp.getId());
       if(temp != null){
        if(this.abm.addToNewList(temp)){
            refreshUserTable();
        }else{
            JOptionPane.showMessageDialog(this, "无效的id");
        }
        
    }
        
         }                                     
private void jButton2MouseClicked(java.awt.event.MouseEvent evt){
   // User nuser = new EditUserJDialog(null,true).showDialog();
    int selectIndex =this.jTable1.getSelectedRow();
   User user= abm.getByIndex(selectIndex);
   if(user!=null){
   String status = new EditUserJDialog(null,true,user).showDialog();
       
        if( this.abm.setStatus(selectIndex,status)){
            refreshUserTable();
       
            
        }
   }else  JOptionPane.showMessageDialog(this, "请选择一个用户");
}
private void jButton3MouseClicked(java.awt.event.MouseEvent evt){
    //User nuser = new RemoveUserJDialog(null,true).showJDialog();
     
      if(this.abm.Remove(this.jTable1.getSelectedRow())){
            refreshUserTable();
         JOptionPane.showMessageDialog(this, "删除成功");
      }
}
    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                     
        // TODO add your handling code here:
        if(jTable1.getSelectedRowCount() != 0 ){
            jButton2.setEnabled(true);
            jButton3.setEnabled(true);
        }
        
    }                                    

    private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
        
        if(abm.commitNewList()) {
            JOptionPane.showMessageDialog(this, "提交成功");
            refreshUserTable();
        }
    }                                     
     public void refreshSearchTable(String key,String keywords){
        this.userTable.setDataVector(abm.searchTableVector(key, keywords), TableModelFactory.USER_COLUMN_NAMES);
    }
    public void refreshUserTable(){
        this.userTable.setDataVector(abm.userTableVector(), TableModelFactory.USER_COLUMN_NAMES);
    }
    
    SystemManagerFrame SMF;
        
    static private AddUserManage abm = new AddUserManage();
    private DefaultTableModel userTable = TableModelFactory.getAddUserModel(); 
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
